﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using CommonLib;
using CommonLib.Audio;

public class GameInstantiateSample : MonoBehaviour
{

	[SerializeField]
	protected float m_LogoDelay = 2.0f;
	protected bool InitComplete { get; set; }
	
	//程序启动入口
	virtual public void Awake()
	{
		InitComplete = false;

		//1.初始化游戏框架
		Debug.Log("游戏初始化");


		{
			//1.1程序显示启动画面
			//启动协程
			//在调整Camera大小之前不能显示Logo
			GameObject goClient = GameResourceLoader.Instance.LoadFromResource<GameObject>("Prefab/GameClient");
			if( null != goClient)
			{
				ClientBase clientBase = Instantiate(goClient).GetComponent<ClientBase>();
				clientBase.Init();
				DontDestroyOnLoad(clientBase.gameObject);
			}
			StartCoroutine(CorStartUp());
		}
		//2.初始化音频管理
		Debug.Log("音频管理器初始化");
		GameObject go = GameResourceLoader.Instance.LoadFromResource<GameObject>("Prefab/AudioMain");
		if (null != go)
		{
			AudioManagerBase audio = Instantiate(go).GetComponent<AudioManagerBase>();
			//初始化组件
			audio.Init();
			DontDestroyOnLoad(audio.gameObject);
		}


		//3.初始化平台SDK
		Debug.Log("平台管理器");
		GameObject goPlatform = GameResourceLoader.Instance.LoadFromResource<GameObject>("Prefab/Platform");
		if(null != goPlatform)
		{
			GameObject gopf =  Instantiate(goPlatform);
			DontDestroyOnLoad(gopf);
		}
		//final.启动完成 结束标题画面跳转登录界面
		InitComplete = true;

	}

	protected virtual IEnumerator CorStartUp()
	{
		Sprite sprite = Resources.Load<Sprite>("Logo/logo.jpg");
		
		
		yield return new WaitForSeconds(m_LogoDelay);

		while(InitComplete != true)
		{
			yield return new WaitForEndOfFrame();
		}

		//JumpToLogin();
		//经由当前状态跳转
		GameStateStartUp startUp = ClientBase.StateController.CurrentState as GameStateStartUp;
		yield return startUp.EndStartUpState();
	}
		
	
	void JumpToLogin()
	{
		SceneLevelManager.LoadLevel("1_Login");
	}

	/* 程序启动自动添加
	//[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
	static private void GamePreInit()
	{
		if(GameInstantiate.Instance == null)
		{
			GameObject go = new GameObject("GameInstantiate");
		}
	}
	*/
}
